#region Using References

using System;
using System.Diagnostics.Contracts;

#endregion

namespace Beaker
{
	public sealed class ActionOnDispose
		: IDisposable
	{
		private readonly Action _action;
		private bool _isDisposed;

		public ActionOnDispose( Action action )
		{
			Contract.Requires<ArgumentNullException>( action != null );
			_action = action;
		}

		/// <summary>
		/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
		/// </summary>
		/// <filterpriority>2</filterpriority>
		public void Dispose()
		{
			if( !_isDisposed )
				_action();
			_isDisposed = true;
		}
	}
}